using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;
using PersistenceObject.Framework.Expressions;
using System.Data.SqlClient;

namespace PersistenceObject.Framework.ServiceBroker
{
    public class Utility
    {
        private Utility()
        {
            this.spName = "send_message";
            this.paramName = "@sql";
            this.SetSP();
        }
        public static readonly Utility Instance = new Utility();

        private void SetSP()
        {
            try
            {
                string[] str = ConfigurationManager.AppSettings["ServiceBroker"].Split('|');
                spName = str[0];
                paramName = str[1];
            }
            catch { }
        }

        private string spName;
        public string SpName
        {
            get { return spName; }
            set { spName = value; }
        }

        private string paramName;
        public string ParamName
        {
            get { return paramName; }
            set { paramName = value; }
        }

        public string GetParamValue(object value)
        {



            if (value is DBNull)
            {
                return "null";
            }
            if (value is DateTime)
            {
                return string.Format("'{0}'", value.ToString());
            }
            if (value is String)
            {
                return string.Format("'{0}'", value.ToString().Replace("'", "''"));
            }
            if (value is Boolean)
            {
                return (bool)value ? "1" : "0";
            }
            return value.ToString();
        }

        private const string SQL_DELETE = "DELETE FROM {0} ";
        private const string SQL_SELECT = "SELECT {0} FROM {1} ";

        public string AddCommandFilter(string sql, IExpression exp, IDbDriver driver)
        {
            if (exp != null)
            {
                string filter = exp.ServiceBrokerGetFilter(driver);
                if ((filter != null) && (filter.Trim() != ""))
                {
                    sql = string.Concat(sql, " WHERE ", filter);
                }
            }
            return sql;
        }

        public IDbCommand Delete(IDbDriver driver, string tableName, IExpression exp)
        {
            IDbCommand comm = driver.GetCommand();
            comm.CommandType = CommandType.StoredProcedure;
            comm.CommandText = this.SpName;
            Utility.Instance.AddParam(comm, this.AddCommandFilter(string.Format(SQL_DELETE, tableName), exp, driver));
            return comm;
        }

        public void AddParam(IDbCommand comm, string sql)
        {
            IDataParameter param = new SqlParameter(this.ParamName, sql);
            if (!comm.Parameters.Contains(this.ParamName))
            {
                comm.Parameters.Add(param);
            }
            else
            {
                comm.Parameters[this.ParamName] = param;
            }
        }
    }
}
